package com.hn.project.rqinspect.service.impl;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.dingtalk.api.response.OapiUserGetResponse;
import com.dingtalk.api.response.OapiUserGetuserinfoResponse;
import com.hn.common.constant.RedisConstant;
import com.hn.common.enums.RoleIdEnum;
import com.hn.common.exception.BusinessException;
import com.hn.common.utils.CommonUtils;
import com.hn.common.utils.ding.DingUserUtil;
import com.hn.common.utils.sql.SqlUtil;
import com.hn.framework.redis.RedisCache;
import com.hn.framework.web.domain.AjaxResult;
import com.hn.project.rqinspect.domain.database.*;
import com.hn.project.rqinspect.domain.dto.LoginDto;
import com.hn.project.rqinspect.mapper.*;
import com.hn.project.rqinspect.service.SysUserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private UserBranchMapper userBranchMapper;

    @Resource
    private RedisCache redisCache;

    @Value("${upload.web.url}")
    private String uploadWebUrl;

    @Override
    public AjaxResult login(String code) {
        OapiUserGetuserinfoResponse userInfo = DingUserUtil.getUserInfoByCode(code);
        String dingUserid = userInfo.getUserid();
        OapiUserGetResponse oapiUser = DingUserUtil.get(dingUserid);
        System.out.println(JSONUtil.toJsonStr(oapiUser));
        LoginDto loginDto = sysUserMapper.getUserByDingUserId(dingUserid);
        if (loginDto == null) {
            return AjaxResult.error("用户不存在");
        }

        String token = UUID.randomUUID().toString();
        redisCache.setCacheObject(RedisConstant.USER_TOKEN + token, loginDto.getUserId(), 2592000);
        loginDto.setToken(token);

        String userAvatar = loginDto.getUserAvatar();
        if (StrUtil.isBlank(userAvatar)) {
            loginDto.setUserAvatar(CommonUtils.pingJieUrl(userAvatar, uploadWebUrl));
        }

        Integer userId = loginDto.getUserId();
        Integer adminFlag = sysUserMapper.countRoleByUserId(userId, RoleIdEnum.ADMIN.id);
        Integer branchFlag = sysUserMapper.countRoleByUserId(userId, RoleIdEnum.BRANCH.id);
        Integer checkCadreFlag = sysUserMapper.countRoleByUserId(userId, RoleIdEnum.GANBU.id);
        loginDto.setAdminFlag(adminFlag);
        loginDto.setBranchFlag(branchFlag);
        loginDto.setCheckCadreFlag(checkCadreFlag);
        return AjaxResult.success(loginDto);
    }

    @Override
    public SysUser getUserById(Integer userId) {
        return sysUserMapper.getUserById(userId);
    }

    @Override
    public LoginDto getUserInfo(Integer userId) {
        LoginDto loginDto = sysUserMapper.getUserInfo(userId);
        if (loginDto == null) {
            throw new BusinessException("用户不存在");
        }
        String userAvatar = loginDto.getUserAvatar();
        if (StrUtil.isBlank(userAvatar)) {
            loginDto.setUserAvatar(CommonUtils.pingJieUrl(userAvatar, uploadWebUrl));
        }

        Integer adminFlag = sysUserMapper.countRoleByUserId(userId, RoleIdEnum.ADMIN.id);
        Integer branchFlag = sysUserMapper.countRoleByUserId(userId, RoleIdEnum.BRANCH.id);
        Integer checkCadreFlag = sysUserMapper.countRoleByUserId(userId, RoleIdEnum.GANBU.id);
        loginDto.setAdminFlag(adminFlag);
        loginDto.setBranchFlag(branchFlag);
        loginDto.setCheckCadreFlag(checkCadreFlag);

        if (adminFlag == 1 || branchFlag == 1) {
            String branchNames = userBranchMapper.listBranchNamesByUserId(userId);
            loginDto.setBranchNames(branchNames);
        }

        return loginDto;
    }
}
